package com.nefu.editor.web;

import com.nefu.editor.common.EdiException;
import com.nefu.editor.common.ErrorMessage;
import com.nefu.editor.common.RestData;
import com.nefu.editor.common.util.JsonUtil;
import com.nefu.editor.common.util.TokenUtil;
import com.nefu.editor.core.model.Article;
import com.nefu.editor.core.model.User;
import com.nefu.editor.core.model.Vo.ArticleVo;
import com.nefu.editor.service.ArticleService;
import com.nefu.editor.service.WordCloudService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author chenchenT
 * @date : 18-11-13
 * @since : java 8
 */
@CrossOrigin(origins = "*", allowCredentials = "true", allowedHeaders = "*")
@RestController
public class EditorApi {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final WordCloudService wordCloudService;
    private final ArticleService articleService;

    @Autowired
    public EditorApi(WordCloudService wordCloudService, ArticleService articleService) {
        this.wordCloudService = wordCloudService;
        this.articleService = articleService;
    }

    @RequestMapping(value = "/wordCloud", method = RequestMethod.GET)
    public RestData getCollectList(HttpServletRequest request) {
        logger.info("collectList : ");
        User currentUser = TokenUtil.getUserByToken(request);
        if (null != currentUser) {
            List<Map<String, Object>> wordClouds = wordCloudService.getWordCloud();
            return new RestData(wordClouds);
        } else {
            return new RestData(2, ErrorMessage.PLEASE_RELOGIN);
        }
    }

    @RequestMapping(value = "/keyWord", method = RequestMethod.GET)
    public RestData getArticles(ArticleVo articleVo, HttpServletRequest request) {
        logger.info("getArticles : " + JsonUtil.getJsonString(articleVo));
        User currentUser = TokenUtil.getUserByToken(request);
        if (null != currentUser) {
            try {
                List<Map<String, Object>> articles = articleService.getArticleListByKeyWord(articleVo);
                return new RestData(articles);
            } catch (EdiException e) {
                return new RestData(1, e.getMessage());
            }
        } else {
            return new RestData(2, ErrorMessage.PLEASE_RELOGIN);
        }
    }

    @RequestMapping(value = "/detail/{systemId}", method = RequestMethod.GET)
    public RestData getLocation(@PathVariable(value = "systemId") Integer systemId, HttpServletRequest request) {
        logger.info("getLocation : ");
        User currentUser = TokenUtil.getUserByToken(request);
        if (null != currentUser) {
            try {
                Article article = new Article();
                article.setSystemId(systemId);
                Map<String, Object> data = articleService.getArticleDetailByArticleSystemId(article);
                return new RestData(data);
            } catch (EdiException e) {
                return new RestData(1, e.getMessage());
            }
        } else {
            return new RestData(2, ErrorMessage.PLEASE_RELOGIN);
        }
    }

}
